package resolver

// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.

import (
	"context"
	"fmt"
	"github.com/sirupsen/logrus"
	"gqlgen-test2/graph/generated"
	"gqlgen-test2/graph/model"
	"gqlgen-test2/server/constant"
)

func (r *mutationResolver) CreateDriverInfo(ctx context.Context, input *model.NewDriverInfo) (*model.DriverInfo, error) {
	v1, err := r.Gen.NewV1()
	if err != nil {
		return nil, err
	}

	l, ok := ctx.Value(constant.CONTEXT_LOG).(*logrus.Entry)
	if ok {
		l.Info("你好")
	}
	d := &model.DriverInfo{
		DriverID:     v1.String(),
		DriverName:   input.DriverName,
		EnterpriseID: input.EnterpriseID,
		IDCardNumber: input.IDCardNumber,
		Remark:       input.Remark,
		Telephone:    input.Telephone,
	}
	r.Db.WithContext(ctx).Save(d)

	return d, nil
}

func (r *mutationResolver) UpdateDriverInfo(ctx context.Context, input *model.UpdateDriverInfo) (*model.DriverInfo, error) {
	d := model.DriverInfo{
		ID: input.ID,
	}
	r.Db.Model(&d).Updates(model.DriverInfo{
		DriverName:   input.DriverName,
		EnterpriseID: input.EnterpriseID,
		IDCardNumber: input.IDCardNumber,
		Remark:       input.Remark,
		Telephone:    input.Telephone,
	})
	r.Db.Model(&d).First(&d)
	return &d, nil
}

func (r *mutationResolver) DeleteDriverInfo(ctx context.Context, input *model.DeleteDriverInfo) (*model.DeleteResult, error) {
	r.Db.Delete(&model.DriverInfo{}, input.ID)
	return &model.DeleteResult{
		Success:    true,
		FailReason: nil,
	}, nil
}

func (r *mutationResolver) CreateEnterprise(ctx context.Context, input *model.NewEnterPrise) (*model.EnterPrise, error) {
	enterPrise := model.EnterPrise{
		Name: input.Name,
	}
	r.Db.Create(&enterPrise)
	return nil, nil
}

func (r *mutationResolver) UpdateEnterprise(ctx context.Context, input *model.UpdateEnterPrise) (*model.EnterPrise, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *mutationResolver) DeleteEnterprise(ctx context.Context, input *model.DeleteEnterPrise) (*model.DeleteResult, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *queryResolver) DriverInfos(ctx context.Context) ([]*model.DriverInfo, error) {
	infos := make([]*model.DriverInfo, 0)
	r.Db.Model(&model.DriverInfo{}).Find(&infos)
	return infos, nil
}

func (r *queryResolver) Enterprises(ctx context.Context) ([]*model.EnterPrise, error) {
	panic(fmt.Errorf("not implemented"))
}

// Mutation returns generated.MutationResolver implementation.
func (r *Resolver) Mutation() generated.MutationResolver { return &mutationResolver{r} }

// Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }

type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
